package com.fbs.wowo.exception;

import com.fbs.wowo.base.Response;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局异常处理
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private void log(Exception e) {
	    logger.error(e.getMessage(), e);
    }
	/**
	 * 鉴权失败
	 * @return
	 */
	@ExceptionHandler(AuthorizationException.class)
	@ResponseStatus(value=HttpStatus.FORBIDDEN)
	public Response authorizationException(AuthorizationException e) {
        log(e);
		return Response.failed(ErrorCode.ERR_ACCESS_REFUSE.getMsg(), ErrorCode.ERR_ACCESS_REFUSE.getStatus());
	}

	/**
	 * 授权失败
	 */
	@ExceptionHandler(AuthenticationException.class)
	@ResponseStatus(value=HttpStatus.FORBIDDEN)
	public Response authenticationException(AuthenticationException e) {
		log(e);
		return Response.failed(ErrorCode.ERR_ACCESS_REFUSE.getMsg(), ErrorCode.ERR_ACCESS_REFUSE.getStatus());
	}

	@ExceptionHandler(CreateEntityFailedException.class)
//	@ResponseStatus(value=HttpStatus.BAD_REQUEST)
	public Response createEntityFailedException(CreateEntityFailedException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}
	
	@ExceptionHandler(EntityHasExistException.class)
	public Response entityHasExistException(EntityHasExistException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}
	
	@ExceptionHandler(GivenNullParamException.class)
	public Response givenNullParamException(GivenNullParamException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}
	
	@ExceptionHandler(NoSuchDataException.class)
	public Response noSuchDataException(NoSuchDataException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}
	
	@ExceptionHandler(DataOverflowException.class)
	public Response dataOverflowException(DataOverflowException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}

	@ExceptionHandler(InputVerifyFailedException.class)
	public Response inputVerifyFailedException(InputVerifyFailedException e) {
        log(e);
        return Response.failed(e.getMsg(), e.getStatus());
	}
	
	@ExceptionHandler(UnloginException.class)
    @ResponseStatus(value=HttpStatus.FORBIDDEN)
	public Response unloginException(UnloginException e) {
        log(e);
		return Response.failed(ErrorCode.ERR_UN_LOGIN.getMsg(), ErrorCode.ERR_UN_LOGIN.getStatus());
	}
	
	/**
	 * 上传异常
	 */
	@ExceptionHandler(UploadException.class)
    public Response uploadException(UploadException e) {
        log(e);
		return Response.failed(ErrorCode.ERR_UPLOAD.getMsg(), ErrorCode.ERR_UPLOAD.getStatus());
	}
}
